﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Catering.DisplayBoards
{
    public partial class ProductionBoard : System.Web.UI.Page
    {
        Catering_Rules.ClassInfo tClassInfo = new Catering_Rules.ClassInfo();//班组管理
        Catering_Rules.XianQingHuiBao tXianQingHuiBao = new Catering_Rules.XianQingHuiBao();//险情汇报
        Catering_Rules.YingJiQingKuang tYingJiQingKuang = new Catering_Rules.YingJiQingKuang();//应急情况
        Catering_Rules.PeiXunQingKuang tPeiXunQingKuang = new Catering_Rules.PeiXunQingKuang();//培训情况
        Catering_Rules.DailyManagement tDailyManagement = new Catering_Rules.DailyManagement();//日常动态
        Catering_Rules.YuanLiaoYanShou tYuanLiaoYanShou = new Catering_Rules.YuanLiaoYanShou();//原料验收
        Catering_Rules.Dynamic tDynamic = new Catering_Rules.Dynamic();//
        Catering_Rules.Dynamics tDynamics = new Catering_Rules.Dynamics();//服务人员
        Catering_Rules.AttendanceRecord tAttendanceRecord = new Catering_Rules.AttendanceRecord();//倒班人员

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RepDataBind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void RepDataBind()
        {
            DateTime SearchDateTime = DateTime.Today.AddDays(-1);
            if (Request.QueryString["Date"] != null)
            {
                SearchDateTime = Convert.ToDateTime(Request.QueryString["Date"]);
            }

            int AreaID = int.Parse(Request.QueryString["AreaID"].ToString()); //区域ID
            List<Catering_Model.ClassInfo> ClassInfoList = tClassInfo.GetModelList("");//获取对应班组集合


            //险情事件
            DataTable XianQingHuiBaoDt = tXianQingHuiBao.GetList("ribaobianhao like '%" + SearchDateTime.ToShortDateString() + ",%'").Tables[0];
            XianQingHuiBaoDt.Columns.Add("className");
            for (int i = 0; i < XianQingHuiBaoDt.Rows.Count; i++)
            {
                try
                {
                    String rid = XianQingHuiBaoDt.Rows[i]["ribaobianhao"].ToString();
                    if (!String.IsNullOrEmpty(rid))
                    {
                        int classID = Convert.ToInt32(rid.Split(',')[1]);
                        XianQingHuiBaoDt.Rows[i]["className"] = tClassInfo.GetModel(classID).CName;
                    }
                }
                catch
                {
                    continue;
                }
            }
            this.Rep_List.DataSource = XianQingHuiBaoDt;
            this.Rep_List.DataBind();

            //应急情况
            DataTable YingJiQingKuangDt = tYingJiQingKuang.GetList("ribaobianhao like '%" + SearchDateTime.ToShortDateString() + ",%'").Tables[0];
            YingJiQingKuangDt.Columns.Add("className");
            for (int i = 0; i < YingJiQingKuangDt.Rows.Count; i++)
            {
                try
                {
                    String rid = YingJiQingKuangDt.Rows[i]["ribaobianhao"].ToString();
                    if (!String.IsNullOrEmpty(rid))
                    {
                        int classID = Convert.ToInt32(rid.Split(',')[1]);
                        YingJiQingKuangDt.Rows[i]["className"] = tClassInfo.GetModel(classID).CName;
                    }
                }
                catch
                {
                    continue;
                }
            }
            this.Rep_YingJi.DataSource = YingJiQingKuangDt;
            this.Rep_YingJi.DataBind();

            //培训情况
            DataTable PeiXunQingKuangDt = tPeiXunQingKuang.GetList("ribaobianhao like '%" + SearchDateTime.ToShortDateString() + ",%'").Tables[0];
            PeiXunQingKuangDt.Columns.Add("className");
            for (int i = 0; i < PeiXunQingKuangDt.Rows.Count; i++)
            {
                try
                {
                    String rid = PeiXunQingKuangDt.Rows[i]["ribaobianhao"].ToString();
                    if (!String.IsNullOrEmpty(rid))
                    {
                        int classID = Convert.ToInt32(rid.Split(',')[1]);
                        PeiXunQingKuangDt.Rows[i]["className"] = tClassInfo.GetModel(classID).CName;
                    }
                }
                catch
                {
                    continue;
                }
            }
            this.Rep_PeiXun.DataSource = PeiXunQingKuangDt;
            this.Rep_PeiXun.DataBind();
            //原料验收
            DataTable YuanLiaoYanShouDT = tYuanLiaoYanShou.GetList("ribaobianhao like '%" + SearchDateTime.ToShortDateString() + ",%' and yanshou = 1").Tables[0];
            YuanLiaoYanShouDT.Columns.Add("className");
            for (int i = 0; i < YuanLiaoYanShouDT.Rows.Count; i++)
            {
                try
                {
                    String rid = YuanLiaoYanShouDT.Rows[i]["ribaobianhao"].ToString();
                    if (!String.IsNullOrEmpty(rid))
                    {
                        int classID = Convert.ToInt32(rid.Split(',')[1]);
                        YuanLiaoYanShouDT.Rows[i]["className"] = tClassInfo.GetModel(classID).CName;
                    }
                }
                catch
                {
                    continue;
                }
            }
            this.Rep_YanShou.DataSource = YuanLiaoYanShouDT;
            this.Rep_YanShou.DataBind();

            DataTable DaoBanDT = tAttendanceRecord.GetList("Date = '" + SearchDateTime + "' and State = 2").Tables[0];
            List<string> liCName = new List<string>();
            for (int i = 0; i < DaoBanDT.Rows.Count; i++)
            {
                if (liCName.Count == 0)
                    liCName.Add(DaoBanDT.Rows[i]["ClassID"].ToString());
                else
                {
                    bool isFlag = true;
                    for (int iID = 0; iID < liCName.Count;iID++ )
                    {
                        if (liCName[iID] == DaoBanDT.Rows[i]["ClassID"].ToString())
                            isFlag = false;
                    }
                    if(isFlag)
                        liCName.Add(DaoBanDT.Rows[i]["ClassID"].ToString());
                }
            }
            DataTable dt_need = new DataTable();
            dt_need.Columns.Add("ClassName");
            dt_need.Columns.Add("upNumber");
            dt_need.Columns.Add("downNumber");
            for (int i = 0; i < liCName.Count; i++)
            {
                try
                {
                    DataRow dr = dt_need.NewRow();
                    dr["ClassName"] = tClassInfo.GetModel(int.Parse(liCName[i])).CName;
                    dr["upNumber"] = DaoBanDT.Select("ClassID = " + liCName[i] + " and dbstate = 1").Count();
                    dr["downNumber"] = DaoBanDT.Select("ClassID = " + liCName[i] + " and dbstate = 0").Count();
                    dt_need.Rows.Add(dr);
                }
                catch
                {
                    continue;
                }
            }
            this.Rep_Daoban.DataSource = dt_need;
            this.Rep_Daoban.DataBind();

            //服务数据
            List<E_ServiceData> Servicelist = new List<E_ServiceData>();
            foreach (Catering_Model.ClassInfo eClassInfo in ClassInfoList)
            {
                E_ServiceData Item = new E_ServiceData();
                Item.ClassName = eClassInfo.CName;

                string strWhere = "";           //配餐条件
                string strWhere_Dynamic = "";   //主表条件
                string strAttendanceRecord = "";//配餐人员
                strWhere = "AND ClassID=" + eClassInfo.id + " and Dates=cast('" + SearchDateTime.ToShortDateString() + "' as datetime)";
                strWhere_Dynamic = " ClassID=" + eClassInfo.id + " and [Date]=cast('" + SearchDateTime.ToShortDateString() + "' as datetime)";
                strAttendanceRecord = " ClassID=" + eClassInfo.id + " and State=1 and [Date]=cast('" + SearchDateTime.ToShortDateString() + "' as datetime)";

                int pcrs = 0;   //配餐人数
                int dy = 0;     //定员人数
                int qtrs0 = 0;  //其他人数
                int jcrs = 0;   //就餐人数
                int dbrs = 0;   //倒班人数

                //其他单位人数
                DataTable dt_Dynamics = tDynamics.GetListGroupByDanWei(strWhere, "temp1").Tables[0];
                if (dt_Dynamics != null && dt_Dynamics.Rows.Count > 0)
                {
                    foreach (DataRow row in dt_Dynamics.Rows)
                    {
                        qtrs0 += Convert.ToInt32(row["Eat"].ToString());
                    }
                }

                //人员动态
                DataTable dt_Dynamic = tDynamic.GetList(strWhere_Dynamic).Tables[0];
                if (dt_Dynamic != null && dt_Dynamic.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt_Dynamic.Rows)
                    {
                        dy += Convert.ToInt32(dr["persons"]);
                    }
                }

                //配餐人员
                DataTable dt_AttendanceRecord = tAttendanceRecord.GetList(strAttendanceRecord).Tables[0];
                if (dt_AttendanceRecord != null && dt_AttendanceRecord.Rows.Count > 0)
                {
                    pcrs = dt_AttendanceRecord.Rows.Count;
                }

                //就餐人数
                jcrs = tDynamics.GetEatSum(eClassInfo.id, Convert.ToDateTime(SearchDateTime.ToShortDateString()));

                //倒班人数
                DataTable dt_daoban = new Catering_Rules.AttendanceRecord().GetList(" ClassID=" + eClassInfo.id + " AND Date=cast('" + SearchDateTime.ToShortDateString() + "' as datetime) AND State=2  ").Tables[0];
                if (dt_daoban != null && dt_daoban.Rows.Count > 0)
                {
                    dbrs = dt_daoban.Rows.Count;
                }

                Item.ServiceSum = jcrs + dy;    //服务人数=就餐人数+ 平台定员=收费总人数
                Item.OnthejobSum = pcrs;        //在岗人数=配餐人数
                Item.ChangeshiftsSum = dbrs;    //倒班人数
                Item.EatSum = jcrs;

                int sfry = dy + qtrs0;
                Item.Sfry = sfry;
                /*
                int Temppcrs = pcrs;
                while (sfry % 2 == 0 && Temppcrs % 2 == 0 && sfry != 0 && Temppcrs!=0)
                {
                    sfry = sfry / 2;
                    Temppcrs = Temppcrs / 2;
                }
                while (sfry % 3 == 0 && Temppcrs % 3 == 0 && sfry != 0 && Temppcrs != 0)
                {
                    sfry = sfry / 3;
                    Temppcrs = Temppcrs / 3;
                }
                Item.ServiceProportion = Temppcrs + "：" + sfry;    //服务比例=收费总人数/配餐人数
                */
                Servicelist.Add(Item);
            }

            List<E_ServiceData> ServicelistNew = new List<E_ServiceData>();
            E_ServiceData eServiceData = new E_ServiceData();
            eServiceData.ServiceSum = Servicelist.Sum(p => p.ServiceSum);
            eServiceData.OnthejobSum = Servicelist.Sum(p => p.OnthejobSum);
            eServiceData.ChangeshiftsSum = Servicelist.Sum(p => p.ChangeshiftsSum);
            eServiceData.EatSum = Servicelist.Sum(p => p.EatSum);
            eServiceData.Sfry = Servicelist.Sum(p => p.Sfry);

            int sfryNew = eServiceData.Sfry;
            int TemppcrsNew = eServiceData.OnthejobSum;
            while (sfryNew % 2 == 0 && TemppcrsNew % 2 == 0 && sfryNew != 0 && TemppcrsNew != 0)
            {
                sfryNew = sfryNew / 2;
                TemppcrsNew = TemppcrsNew / 2;
            }
            while (sfryNew % 3 == 0 && TemppcrsNew % 3 == 0 && sfryNew != 0 && TemppcrsNew != 0)
            {
                sfryNew = sfryNew / 3;
                TemppcrsNew = TemppcrsNew / 3;
            }
            if (TemppcrsNew != 0) 
                eServiceData.ServiceProportion = "1：" + ( (float) Convert.ToDouble(sfryNew) / (float)Convert.ToDouble(TemppcrsNew)).ToString("F1");    //服务比例=收费总人数/配餐人数
            ServicelistNew.Add(eServiceData);

            this.Rep_Service.DataSource = ServicelistNew;
            this.Rep_Service.DataBind();

            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, Page.GetType(), "script", "<script>$(function () { $(\".Table_2>tbody tr:odd\").addClass(\"Table_Tr_odd\"); });</script>", false);
        }

        //定时刷新页面
        protected void Btn_Ref_Click(object sender, EventArgs e)
        {
            this.RepDataBind();
        }
    }

    /// <summary>
    /// 服务数据
    /// </summary>
    public class E_ServiceData
    {
        /// <summary>
        /// 班组名称
        /// </summary>
        public string ClassName { get; set; }

        /// <summary>
        /// 服务人数= 就餐人数+ 平台定员=收费总人数
        /// </summary>
        public int ServiceSum { get; set; }

        /// <summary>
        /// 在岗人数=配餐人数
        /// </summary>
        public int OnthejobSum { get; set; }

        /// <summary>
        /// 倒班人数
        /// </summary>
        public int ChangeshiftsSum { get; set; }

        /// <summary>
        /// 服务比例=收费总人数/配餐人数
        /// </summary>
        public string ServiceProportion { get; set; }

        /// <summary>
        /// 就餐人数
        /// </summary>
        public int EatSum { get; set; }

        /// <summary>
        /// 收费人员
        /// </summary>
        public int Sfry { get; set; }
    }

}